layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#albumList',
        url : 'list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 5,
        limits : [10,15,20,25],
        id : "albumListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'name', title: '相册名称', align:"left"},
            {field: 'showImg', title: '封面图', width:180, align:"center",templet:function(d){
                    return '<img style="width: 100px;" src="/upload/album/cover/'+d.showImg+'" height="26" />';
                }},

            {field: 'showFlag', title: '是否公开', align:'center', templet:function(d){
                    return '<input type="checkbox" name="showFlag" lay-filter="showFlag" lay-skin="switch" switch_id="'+d.id+'" lay-text="是|否" '+d.showFlag+'>'
                }},
            {field: 'creator', title: '创建人', width:150},
            {field: 'createTime', title: '创建时间', align:'center'},

            {field: 'imgNum', title: '照片数量（张）', width:150 ,align:'center'},
            {title: '操作', width:250, templet:'#albumListBar',fixed:"right",align:"center"}
        ]]
    });

    //是否首页展示
    form.on('switch(showFlag)', function(data){
        var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            //自定义switch_id 作为主键 用作修改内容唯一标识
            var id = data.elem.attributes['switch_id'].nodeValue;
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "albumshow",//url
                data: {
                    id: id,
                },
                success: function (result) {
                    if (result.flag == "Y" || result.flag == "N") {
                        layer.msg("操作成功！");
                    } else{
                        layer.msg("操作失败！", {icon: 2});
                    }
                    tableIns.reload();
                }, error: function () {
                    layer.msg("操作失败，请重试！", {icon: 2});
                }
            });
        },500);
    })


    //添加文章
    function addAlbum(){
        var index = layui.layer.open({
            title : "新建相册",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","500px"],
            content : "albumAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    function updateAlbum(data){
        var index = layui.layer.open({
            title : "修改相册信息",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","500px"],
            content : "albumUpdate?id="+data.id,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    $(".addAlbum_btn").click(function(){
        addAlbum();
    })

    $(".editAlbumImg_btn").click(function(){

        var checkStatus = table.checkStatus('albumListTable'),
            data = checkStatus.data;

        if(data.length == 1) {
            var id = data[0].id;

            var win = window.open();
            $.ajax({
                url: 'albumImgEdit',
                type: 'post',
                data: {
                    id : id
                },
                asyn: false
            }).done(function(data) {
                var doc = win.document;
                doc.write(data);    // !!! 关键，接收后端的数据并进行新窗口写入
                doc.close();
            });


        }else if(data.length > 1){
            layer.msg("只能选择一条记录进行内容管理，请重新选择！");
        }else{
            layer.msg("请选择一条记录！");
        }
    })


    //列表操作
    table.on('tool(albumList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            updateAlbum(data);
        } else if(layEvent === 'del'){
            //删除
            if(data.imgNum > 0){
                layer.msg("该相册下存在照片，不允许删除！",{icon: 5});
                return;
            }else{
                layer.confirm('确定删除此相册？',{icon:3, title:'提示信息'},function(index){
                    $.post("delete",{
                        id : data.id  //将需要删除的newsId作为参数传入
                    },function(data){
                        data = JSON.parse(data)
                        if(data.flag ==="success"){
                            layer.msg("删除成功");
                            tableIns.reload();
                            layer.close(index);
                        }else if(data.flag === "permissions"){
                            layer.msg("权限不足，无法删除！",{icon: 5});
                        }else{
                            layer.msg("删除失败！", {icon: 2});
                        }
                    })
                });
            }
        }
    });
})